package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityData;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityParamVersion;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbCapacityParamVersionDao {

    @Results(id = "pcbCapacityParamVersionResultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "version_year", property = "versionYear", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version_num", property = "versionNum", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version_type", property = "versionType", jdbcType = JdbcType.INTEGER),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_CAPAICTY_PARAM_VERSION +
            "<where> " +
            "<if test='id != null'>" +
            " and id = #{id}" +
            "</if>" +
            "<if test='versionYear != null and versionYear != \"\"'>" +
            " and version_year = #{versionYear}" +
            "</if>" +
            "<if test='versionNum != null'>" +
            " and version_num = #{versionNum}" +
            "</if>" +
            "<if test='versionType != null'>" +
            " and version_type = #{versionType}" +
            "</if>" +
            "</where> " +
            " order by version_year,version_num " +
            "</script>")
    List<PcbCapacityParamVersion> getByLimit(@Param("id") Integer id, @Param("versionYear") String versionYear,
                                             @Param("versionNum") String versionNum,
                                             @Param("versionType") Integer versionType);

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_CAPAICTY_PARAM_VERSION +
            "<where> " +
            "<if test='versionYear != null and versionYear != \"\"'>" +
            " and version_year = #{versionYear}" +
            "</if>" +
            "<if test='versionType != null'>" +
            " and version_type = #{versionType}" +
            "</if>" +
            "</where> " +
            " order by version_num desc limit 1" +
            "</script>")
    @ResultMap(value = "pcbCapacityParamVersionResultMap")
    PcbCapacityParamVersion getNewestByVersionYear(@Param("versionYear") String versionYear,
                                                   @Param("versionType") Integer versionType);

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_CAPAICTY_PARAM_VERSION +
            "<where> " +
            "<if test='versionType != null'>" +
            " and version_type = #{versionType}" +
            "</if>" +
            "</where> " +
            " order by version_year desc,version_num desc limit 1" +
            "</script>")
    @ResultMap(value = "pcbCapacityParamVersionResultMap")
    PcbCapacityParamVersion getNewest(@Param("versionType") Integer versionType);

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_CAPAICTY_PARAM_VERSION + " (version_year, version_num, create_time, create_user, update_time, update_user, status, sort, version_type) values " +
            "(#{versionYear}, #{versionNum}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort}, #{versionType})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(PcbCapacityParamVersion param);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_PARAM_VERSION + " where id = #{id} ")
    Integer delete(@Param("id") Integer id);

    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_PARAM_VERSION + " where version_year = #{versionYear} and version_num =#{versionNum} ")
    Integer deleteByVersion(@Param("versionYear") String versionYear, @Param("versionNum") String versionNum);
}
